﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using System.Data;
using System.Data.SqlClient;
using Yipusoft.ProfessionalTeam.DAL;

public partial class Combat_ProfessionalTeam_ProfessionalStat : System.Web.UI.Page
{
    bool bl = true;
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
            BindTable();
    }

    private void BindTable()
    {
        string sql = "";
        int ProCount = 0;
        int HelpCount = 0;
        int AssCount = 0;

        int ProCount1 = 0;
        int HelpCount1 = 0;
        int AssCount1 = 0;

        TableRow tr;
        TableCell cel;
        tr = new TableRow();
        cel = new TableCell();

        cel.RowSpan = 2;
        tr.Cells.Add(cel);
        cel = new TableCell();
        cel.Text = "灭火救援攻坚组";
        cel.ColumnSpan = 2;
        tr.Cells.Add(cel);
        cel = new TableCell();
        cel.ColumnSpan = 2;
        cel.Text = "抢险救援班";
        tr.Cells.Add(cel);

        cel = new TableCell();
        cel.ColumnSpan = 2;
        cel.Text = "抢险救援专业队";
        tr.Cells.Add(cel);
        tb.Rows.Add(tr);
        tr = new TableRow();     
        
        string text;
        for (int i = 2; i <= 7; i++)
        {

            if (i % 2 == 0)
            {
                text = "应建";
            }
            else
            {
                text = "实建";
            }
            cel = new TableCell();
            cel.Text = text;
            tr.Cells.Add(cel);
        }
        tb.Rows.Add(tr);
        TeamDataContext tdc = new TeamDataContext();
        DataTable dtb= Yipusoft.Account.DBUtility.SqlHelper.ExecuteDataset(new SqlConnection(Yipusoft.BasicBase.DBUtility.SqlHelper.CONN_STRING_FIREBASE), CommandType.Text, "Select [DepartmentID], [gjz] ,[jyb] ,[zyd] from  ProfessionalBase").Tables[0];
        List<Yipusoft.Account.Model.DepartmentInfo> departmentlist= Yipusoft.Account.BLL.Department.GetList(" Right(DepartmentID,3)='000' And Left(DepartmentID,4)<>'1300' ");
        for (int i = 0; i < 12; i++)
        {
            tr = new TableRow();
            if (i < dtb.Rows.Count)
            {
                Yipusoft.Account.Model.DepartmentInfo departmentInfo = departmentlist.Where(o => o.DepartmentID == dtb.Rows[i][0].ToString()).FirstOrDefault();
                cel = new TableCell();
                cel.Text = "<a href='HelpStat.aspx?DepartmentID=" + departmentInfo.DepartmentID + "'>" + departmentInfo.ShortName + "</a>";
                tr.Cells.Add(cel);

                //灭火攻坚组
                //应建
                cel = new TableCell();
                cel.Text = dtb.Rows[i][1].ToString() ;
                AssCount1 += int.Parse(dtb.Rows[i][1].ToString());
                tr.Cells.Add(cel);
                //实建
                int a = GetAssaults(departmentInfo.DepartmentID);
                AssCount += a;
                sql = "Update ProfessionalBase Set gjz1=" + a + " Where DepartmentID='" + departmentInfo.DepartmentID + "'";
                Yipusoft.Account.DBUtility.SqlHelper.ExecuteNonQuery(Yipusoft.BasicBase.DBUtility.SqlHelper.CONN_STRING_FIREBASE, CommandType.Text, sql);
                cel = new TableCell();
                cel.Text = "<a href='HelpStatOrder.aspx?DepartmentID=" + departmentInfo.DepartmentID + "&type=0'>" + a.ToString() + "</a>";
                tr.Cells.Add(cel);

                //抢险救援班
                //应建
                cel = new TableCell();
                cel.Text = dtb.Rows[i][2].ToString();
                HelpCount1 += int.Parse(dtb.Rows[i][2].ToString());
                tr.Cells.Add(cel);
                //实建
                int h = GetHelpCount(departmentInfo.DepartmentID);
                HelpCount += h;
                cel = new TableCell();
                sql = "Update ProfessionalBase Set jyb1=" + h + " Where DepartmentID='" + departmentInfo.DepartmentID + "'";
                Yipusoft.Account.DBUtility.SqlHelper.ExecuteNonQuery(Yipusoft.BasicBase.DBUtility.SqlHelper.CONN_STRING_FIREBASE, CommandType.Text, sql);
                cel.Text = "<a href='HelpStatOrder.aspx?DepartmentID=" + departmentInfo.DepartmentID + "&type=1'>"+ h.ToString() + "</a>";
                tr.Cells.Add(cel);

                //抢险救援专业队
                //应建
                cel = new TableCell();
                cel.Text = dtb.Rows[i][3].ToString();
                ProCount1 += int.Parse(dtb.Rows[i][3].ToString());
                tr.Cells.Add(cel);
                //实建
                cel = new TableCell();
                int p=GetProCount(departmentInfo.DepartmentID);
                ProCount += p;
                sql = "Update ProfessionalBase Set zyd1=" + p + " Where DepartmentID='" + departmentInfo.DepartmentID + "'";
                Yipusoft.Account.DBUtility.SqlHelper.ExecuteNonQuery(Yipusoft.BasicBase.DBUtility.SqlHelper.CONN_STRING_FIREBASE, CommandType.Text, sql);
                cel.Text = "<a href='HelpStatOrder.aspx?DepartmentID=" + departmentInfo.DepartmentID + "&type=2'>" + p.ToString() + "</a>";
                tr.Cells.Add(cel);

                tb.Rows.Add(tr);

            }
            else
            {
                cel = new TableCell();
                cel.Text = "合计";
                tr.Cells.Add(cel);
                //应建
                cel = new TableCell();
                cel.Text = AssCount1.ToString();
                tr.Cells.Add(cel);
                //实建
                cel = new TableCell();
                cel.Text = AssCount.ToString();
                tr.Cells.Add(cel);

                //应建
                cel = new TableCell();
                cel.Text = HelpCount1.ToString();
                tr.Cells.Add(cel);
                //实建
                cel = new TableCell();
                cel.Text = HelpCount.ToString();
                tr.Cells.Add(cel);

                //应建
                cel = new TableCell();
                cel.Text = ProCount1.ToString();
                tr.Cells.Add(cel);
                //实建
                cel = new TableCell();
                cel.Text = ProCount.ToString();
                tr.Cells.Add(cel);
                tb.Rows.Add(tr);
            }
        }
        
    }

    //获取攻坚组数量
    private int GetAssaults(string departmentID)
    {
        int count = 0;
        if (!bl)
        {
            TeamDataContext tdc = new TeamDataContext();
            List<Assault> assaults = tdc.Assault.Where(o => o.DepartmentId.Substring(0, 4) == departmentID.Substring(0, 4)).ToList();
            foreach (Assault a in assaults)
            {
                if (Yipusoft.ProfessionalTeam.BLL.Stat.isComplianceAssaultByPid(a.Id))
                {
                    count += 1;
                }
            }
            return count;
        }      
        else
        {
            return Convert.ToInt16(Yipusoft.Account.DBUtility.SqlHelper.ExecuteScalar(Yipusoft.BasicBase.DBUtility.SqlHelper.CONN_STRING_FIREBASE,CommandType.Text,"Select gjz1 From ProfessionalBase Where DepartmentID='"+departmentID+"'"));
        }
    }

    //抢险救援班
    private int GetHelpCount(string departmentID)
    {
        int count=0;
        if (!bl)
        {
            List<Yipusoft.Account.Model.DepartmentInfo> departmentList = Yipusoft.Account.BLL.Department.GetList(" SubString(DepartmentID,5,1)='3' And Left(DepartmentID,4)='" + departmentID.Substring(0, 4) + "' And DepartmentID Not in (Select DepartmentID From Account_Departments Where FullName like '%特勤%')");
            foreach (Yipusoft.Account.Model.DepartmentInfo d in departmentList)
            {
                if (Yipusoft.ProfessionalTeam.BLL.Stat.isComplianceHelpByDepartment(d.DepartmentID))
                {
                    count += 1;
                }
            }
            return count;
        }         
        else
        {
            return Convert.ToInt16(Yipusoft.Account.DBUtility.SqlHelper.ExecuteScalar(Yipusoft.BasicBase.DBUtility.SqlHelper.CONN_STRING_FIREBASE,CommandType.Text,"Select jyb1 From ProfessionalBase Where DepartmentID='"+departmentID+"'"));
        }
    }

    //灭火抢险救援专业队伍
    private int GetProCount(string departmentID)
    {

        int count = 0;
        if (!bl)
        {
            TeamDataContext tdc = new TeamDataContext();
            List<ProfessionalProc> procs = tdc.ProfessionalProc.Where(o => o.DepartmentId.Substring(0, 4) == departmentID.Substring(0, 4)).ToList();
            foreach (ProfessionalProc p in procs)
            {
                if (Yipusoft.ProfessionalTeam.BLL.Stat.isComplianceProsByPid(p.id))
                {
                    count += 1;
                }
            }

            return count;
        }
        else
        {
            return Convert.ToInt16(Yipusoft.Account.DBUtility.SqlHelper.ExecuteScalar(Yipusoft.BasicBase.DBUtility.SqlHelper.CONN_STRING_FIREBASE,CommandType.Text,"Select zyd1 From ProfessionalBase Where DepartmentID='"+departmentID+"'"));
        }
       
    }
}
